{extend name="../../common/view/admin_base"/}

{block name="main"}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-body">
            <button class="layui-btn layui-btn-warm">所有用户组</button>
            <div class="layui-btn-group layui-form">
                {if key_exists('admin/group/add',$btns)}
                <a class="layui-btn openfrm" data-href="{:url('admin/group/add')}"><i class="layui-icon layui-icon-add"></i>增加</a>
                {/if}
            </div>
            <table class="layui-table layui-form" id="table" lay-filter="datatable" lay-size="sm"></table>
        </div>
    </div>
</div>

<script type="text/html" id="actionTpl">
    {if isset($btns['admin/group/groupuser']) }
    {assign name="btn" value="$btns['admin/group/groupuser']" /}
    <a class="layui-btn layui-btn-xs groupuser" data-href="{:url($btn['action'])}?id={{d.guid}}"><i class="{$btn['icon']}"></i>{$btn['name']}</a>
    {/if}
    {if isset($btns['admin/group/edit']) }
    {assign name="btn" value="$btns['admin/group/edit']" /}
    <a class="layui-btn layui-btn-xs openfrm" data-href="{:url($btn['action'])}?id={{d.guid}}"><i class="{$btn['icon']}"></i>{$btn['name']}</a>
    {/if}
    {if isset($btns['admin/group/remove']) }
    {assign name="btn" value="$btns['admin/group/remove']" /}
    <a class="layui-btn layui-btn-xs removebtn" data-href="{:url($btn['action'])}?id={{d.guid}}"><i class="{$btn['icon']}"></i>{$btn['name']}</a>
    {/if}
    {if isset($btns['admin/group/setauth']) }
    {assign name="btn" value="$btns['admin/group/setauth']" /}
    <a class="layui-btn layui-btn-xs setauth" data-href="{:url($btn['action'])}?id={{d.guid}}"><i class="{$btn['icon']}"></i>{$btn['name']}</a>
    {/if}
</script>

<script>
    layui.use('table', function(){
        var table = layui.table
            ,form = layui.form;
        table.render({
            elem: '#table'
            ,url:"{:url('admin/group/index')}"
            ,cellMinWidth: 80
            ,cols: [[
                {type:'numbers'}
                ,{field:'name', title:'名称',width:160}
                ,{field:'remark', title:'摘要',width:300}
                ,{field:'action', title:'操作',templet: '#actionTpl', unresize: true}
            ]]
            ,page: false
            ,response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            }
            ,parseData: function(res){ //将原始数据解析成 table 组件所规定的数据
                return {
                    "code": 200,
                    "msg": res.msg,
                    "data": res.data
                };
            }
            ,done: function(res, curr, count){
                $.bindactions();
            }
        });

        $.bindactions = function(){
            $(".openfrm").on('click',function(){
                var href = $(this).data('href');
                top.layer.open({
                    type: 2
                    ,title:'编辑框'
                    ,content: href
                    ,area: ['650px', '360px']
                });
            });

            $(".groupuser").on('click',function(){
                var href = $(this).data('href');
                top.layer.open({
                    type: 2
                    ,title:'编辑框'
                    ,content: href
                    ,area: ['650px', '560px']
                });
            })

            $(".setauth").on('click',function(){
                var href = $(this).data('href');
                top.layer.open({
                    type: 2
                    ,title:'编辑框'
                    ,content: href
                    ,btn: ['确定', '取消']
                    ,area: ['750px', '520px']
                    ,yes: function(index, layero){
                        var ids = [];
                        layero.find('iframe').contents().find('#tree-table').find('.cbx.layui-form-checked').each(function(){
                            ids.push($(this).parent().parent().data('id'));
                        });
                        $.post(href,{auth:ids},function(res){
                            console.log(res);
                        });
                        top.layer.close(index); //关闭弹层
                    }
                });
            });

            $(".removebtn").on('click',function(){
                var url = $(this).data('href');
                top.layer.confirm("确定删除吗？此操作不能撤销！", {icon: 3, title:'提示'},
                    function(index){
                        top.layer.closeAll();
                        $.post(url,function(res){
                            layer.msg(res.msg,{
                                offset: '15px'
                                ,icon: 1
                                ,time: 1000
                            },function(){
                                document.location.reload();
                            });
                        });
                    },function (index) {
                        top.layer.close(index);
                    }
                );
            });
        }

    });
</script>
{/block}